﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Batalha_Estelar
{
    class ControleInimigo : GameObject
    {
        public readonly List<Nave> NavesInimigas = new List<Nave>();

        private const double atrasoNave = 0.2f;
        private double tempoAux;

        NaveFactory naveFactory;
        Nave naveInimiga;

        public ControleInimigo(BatalhaEstelar game)
            : base(game)
        {
            this.naveFactory = new NaveFactory(game);
        }

        public void criarInimigo(GameTime gameTime)
        {
            var config = GameConfig.GetCurrent();
            var tempo = gameTime.TotalGameTime.TotalSeconds;

            if (tempo > tempoAux && NavesInimigas.Count < config.Enemies)
            {
                var r = new Random();
                
                var width = config.FieldWidth;
                var xCenter = (float)(width / 2);
                var displacement = (r.Next() % (int)(width * 0.3)) * GetSignal(r);

                var pIni = new Vector2((float)(xCenter + displacement), 0.0f);

                naveInimiga = naveFactory.CreateEnemyShip(pIni);

                NavesInimigas.Add(naveInimiga);
                tempoAux = gameTime.TotalGameTime.TotalSeconds + atrasoNave;
            }
        }

        public void controleNavesInimigas(GameTime gameTime)
        {
            NavesInimigas.RemoveAll((x) => x.HitFieldBounds);

            if (NavesInimigas.Count > 0)
            {
                NavesInimigas.ForEach((x) =>
                {
                    SetNewDirection(x);
                    x.Update(gameTime);
                    x.Atirar(gameTime, x.Posicao);
                });
            }
        }

        public override void Update(GameTime gameTime)
        {
            controleNavesInimigas(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (NavesInimigas.Count > 0)
            {
                foreach (Nave aux in NavesInimigas)
                {
                    aux.Draw(spriteBatch);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            //nada por enquanto...
            return;
        }

        private void SetNewDirection(Nave ship)
        {
            var dir = new Vector2(0.0f, 1.0f);
            ship.Direcao = dir;
        }

        //Random Direction
        //private void SetNewDirection(Nave ship)
        //{
        //    var r = new Random();

        //    var dx = (float)(r.Next() % 2);
        //    var dy = (float)(r.Next() % 2);

        //    dx *= GetSignal(r);
        //    dy *= GetSignal(r);

        //    var dir = new Vector2(dx, dy);

        //    ship.Direcao = dir;
        //}

        private float GetSignal(Random r)
        {
            return (float)((r.Next() % 2 == 0) ? 1 : -1);
        }

        public void removerTodos()
        {
            NavesInimigas.Clear();
        }
    }
}
